{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from mgwr.sel_bw import Sel_BW\n",
    "from mgwr.gwr import GWR, MGWR\n",
    "import pandas as pd\n",
    "import libpysal as ps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = ps.io.open(ps.examples.get_path('GData_utm.csv'))\n",
    "coords = list(zip(data.by_col('X'), data.by_col('Y')))\n",
    "y = np.array(data.by_col('PctBach')).reshape((-1,1))\n",
    "rural  = np.array(data.by_col('PctRural')).reshape((-1,1))\n",
    "pov = np.array(data.by_col('PctPov')).reshape((-1,1)) \n",
    "black = np.array(data.by_col('PctBlack')).reshape((-1,1))\n",
    "fb = np.array(data.by_col('PctFB')).reshape((-1,1))\n",
    "pop = np.array(data.by_col('TotPop90')).reshape((-1,1))\n",
    "\n",
    "\n",
    "X = np.hstack([fb, black, rural])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bw: 117.0\n",
      "aicc: 299.0508086830288\n",
      "ENP: 11.804769716730096\n",
      "sigma2: 0.3477435474978281\n"
     ]
    }
   ],
   "source": [
    "X = (X - X.mean(axis=0)) / X.std(axis=0)\n",
    "\n",
    "y = y.reshape((-1,1))\n",
    "\n",
    "y = (y - y.mean(axis=0)) / y.std(axis=0)\n",
    "\n",
    "sel = Sel_BW(coords, y, X)\n",
    "\n",
    "bw = sel.search()\n",
    "print('bw:', bw)\n",
    "gwr = GWR(coords, y, X, bw)\n",
    "gwr_results = gwr.fit()\n",
    "print('aicc:', gwr_results.aicc)\n",
    "print('ENP:', gwr_results.ENP)\n",
    "print('sigma2:', gwr_results.sigma2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bw(intercept): 92.0\n",
      "bw(foreign): 101.0\n",
      "bw(african_amer): 136.0\n",
      "bw(rural): 158.0\n",
      "aicc: 297.12013812258783\n",
      "sigma2: 0.34477258292171475\n",
      "ENP(model): 11.368250872698306\n",
      "adj_alpha(model): 0.017592855949398054\n",
      "critical_t(model): 2.399257840857394\n",
      "ENP(intercept): 3.8446710802641415\n",
      "adj_alpha(intercept): 0.013005013681577368\n",
      "critical_t(intercept): 2.512107491068591\n",
      "ENP(foreign): 3.5137708051516503\n",
      "adj_alpha(foreign): 0.01422972719982004\n",
      "critical_t(foreign): 2.4788879239423856\n",
      "ENP(african_amer): 2.258052527889825\n",
      "adj_alpha(african_amer): 0.022142974701622888\n",
      "critical_t(african_amer): 2.3106911297007184\n",
      "ENP(rural): 1.7517564593926895\n",
      "adj_alpha(rural): 0.02854278043726143\n",
      "critical_t(rural): 2.210001836555586\n"
     ]
    }
   ],
   "source": [
    "X = (X - X.mean(axis=0)) / X.std(axis=0)\n",
    "\n",
    "y = y.reshape((-1,1))\n",
    "\n",
    "y = (y - y.mean(axis=0)) / y.std(axis=0)\n",
    "\n",
    "selector = Sel_BW(coords, y, X, multi=True, constant=True)\n",
    "bw = selector.search(multi_bw_min=[2], multi_bw_max=[159])\n",
    "print('bw(intercept):', bw[0])\n",
    "print('bw(foreign):', bw[1])\n",
    "print('bw(african_amer):', bw[2])\n",
    "print('bw(rural):', bw[3])\n",
    "mgwr = MGWR(coords, y, X, selector, constant=True)\n",
    "mgwr_results = mgwr.fit()\n",
    "print('aicc:', mgwr_results.aicc)\n",
    "print('sigma2:', mgwr_results.sigma2)\n",
    "print('ENP(model):', mgwr_results.ENP)\n",
    "print('adj_alpha(model):', mgwr_results.adj_alpha[1])\n",
    "print('critical_t(model):', mgwr_results.critical_tval(alpha=mgwr_results.adj_alpha[1]))\n",
    "alphas = mgwr_results.adj_alpha_j[:,1]\n",
    "critical_ts = mgwr_results.critical_tval()\n",
    "print('ENP(intercept):', mgwr_results.ENP_j[0])\n",
    "print('adj_alpha(intercept):', alphas[0])\n",
    "print('critical_t(intercept):', critical_ts[0])\n",
    "print('ENP(foreign):', mgwr_results.ENP_j[1])\n",
    "print('adj_alpha(foreign):', alphas[1])\n",
    "print('critical_t(foreign):', critical_ts[1])\n",
    "print('ENP(african_amer):', mgwr_results.ENP_j[2])\n",
    "print('adj_alpha(african_amer):', alphas[2])\n",
    "print('critical_t(african_amer):', critical_ts[2])\n",
    "print('ENP(rural):', mgwr_results.ENP_j[3])\n",
    "print('adj_alpha(rural):', alphas[3])\n",
    "print('critical_t(rural):', critical_ts[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===========================================================================\n",
      "Model type                                                         Gaussian\n",
      "Number of observations:                                                 159\n",
      "Number of covariates:                                                     4\n",
      "\n",
      "Global Regression Results\n",
      "---------------------------------------------------------------------------\n",
      "Residual sum of squares:                                             71.793\n",
      "Log-likelihood:                                                    -162.399\n",
      "AIC:                                                                332.798\n",
      "AICc:                                                               335.191\n",
      "BIC:                                                               -713.887\n",
      "R2:                                                                   0.548\n",
      "Adj. R2:                                                              0.540\n",
      "\n",
      "Variable                              Est.         SE  t(Est/SE)    p-value\n",
      "------------------------------- ---------- ---------- ---------- ----------\n",
      "X0                                  -0.000      0.054     -0.000      1.000\n",
      "X1                                   0.458      0.066      6.988      0.000\n",
      "X2                                  -0.084      0.055     -1.525      0.127\n",
      "X3                                  -0.374      0.065     -5.734      0.000\n",
      "\n",
      "Multi-Scale Geographically Weighted Regression (MGWR) Results\n",
      "---------------------------------------------------------------------------\n",
      "Spatial kernel:                                           Adaptive bisquare\n",
      "Criterion for optimal bandwidth:                                       AICc\n",
      "Score of Change (SOC) type:                                     Smoothing f\n",
      "Termination criterion for MGWR:                                       1e-05\n",
      "\n",
      "MGWR bandwidths\n",
      "---------------------------------------------------------------------------\n",
      "Variable             Bandwidth      ENP_j   Adj t-val(95%)   Adj alpha(95%)\n",
      "X0                      92.000      3.845            2.512            0.013\n",
      "X1                     101.000      3.514            2.479            0.014\n",
      "X2                     136.000      2.258            2.311            0.022\n",
      "X3                     158.000      1.752            2.210            0.029\n",
      "\n",
      "Diagnostic information\n",
      "---------------------------------------------------------------------------\n",
      "Residual sum of squares:                                             50.899\n",
      "Effective number of parameters (trace(S)):                           11.368\n",
      "Degree of freedom (n - trace(S)):                                   147.632\n",
      "Sigma estimate:                                                       0.587\n",
      "Log-likelihood:                                                    -135.056\n",
      "AIC:                                                                294.849\n",
      "AICc:                                                               297.120\n",
      "BIC:                                                                332.806\n",
      "R2                                                                    0.680\n",
      "Adjusted R2                                                           0.655\n",
      "\n",
      "Summary Statistics For MGWR Parameter Estimates\n",
      "---------------------------------------------------------------------------\n",
      "Variable                   Mean        STD        Min     Median        Max\n",
      "-------------------- ---------- ---------- ---------- ---------- ----------\n",
      "X0                        0.017      0.171     -0.260      0.058      0.271\n",
      "X1                        0.479      0.216      0.117      0.500      0.722\n",
      "X2                       -0.069      0.036     -0.146     -0.064     -0.014\n",
      "X3                       -0.304      0.019     -0.347     -0.302     -0.266\n",
      "===========================================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "mgwr_results.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
